package com.rxt.thermopro.ui.fragment

import android.os.Bundle
import androidx.core.view.isVisible
import androidx.lifecycle.Observer
import com.app.common.base.BaseFragment
import com.app.common.viewmodel.BaseViewModel
import com.rxt.thermopro.R
import com.rxt.thermopro.app.LocalDataDao
import com.rxt.thermopro.ui.dialog.ProbeAlarmBottomDialog
import com.rxt.thermopro.ui.dialog.TempPreAlarmBottomDialog
import com.rxt.thermopro.ui.dialog.TypeBottomDialog
import com.rxt.thermopro.viewmodel.BBQSettingViewModel
import com.rxt.thermopro.viewmodel.BleMessageViewModel
import com.rxt.thermopro.viewmodel.BleMessageViewModel.Companion.DEVICE_TYPE_BBQ
import com.rxt.thermopro.viewmodel.BleMessageViewModel.Companion.DEVICE_TYPE_GRILL
import kotlinx.android.synthetic.main.fragment_bbq_setting.*
import kotlinx.android.synthetic.main.fragment_setting.backButton

class BBQSettingFragment : BaseFragment<BBQSettingViewModel>() {
    override fun layoutId(): Int {
        return R.layout.fragment_bbq_setting
    }

    companion object {
        private const val ARG_ADDRESS = "address"
        private const val ARG_DEVICE_TYPE = "deviceType"

        fun newInstance(): BBQSettingFragment {
            return BBQSettingFragment()
        }

        fun newInstance(address: String,deviceType:Int): BBQSettingFragment {
            return BBQSettingFragment().apply {
                arguments = Bundle().apply {
                    putString(ARG_ADDRESS, address)
                    putInt(ARG_DEVICE_TYPE, deviceType)
                }
            }
        }
    }

    private var mAddresses=""
    private var mDeviceType=-1
    override fun initView(savedInstanceState: Bundle?) {
        mAddresses=arguments?.getString(ARG_ADDRESS, "") ?: ""
        mDeviceType=arguments?.getInt(ARG_DEVICE_TYPE, -1) ?: -1

        switchNotice.isSelected = LocalDataDao.INSTANCE.isDisconnectNotice(mAddresses)
        switchAlarm.isSelected = LocalDataDao.INSTANCE.isAlarm(mAddresses)
        switchAlarmVibration.isSelected = LocalDataDao.INSTANCE.isAlarmVibration(mAddresses)

        setListener()

        val tempAlarmList = LocalDataDao.INSTANCE.getTempPreAlarmList()
        preAlarmType.text = tempAlarmList[LocalDataDao.INSTANCE.getTempPreAlarm(mAddresses)].alarmSimpleName

        mViewModel.getProbeAlarmList()

        if (mDeviceType== DEVICE_TYPE_GRILL){
            linear5.isVisible=false
            linear6.isVisible=false
        }

        mViewModel.probeAlarmList.observe(this, Observer {
            probe_1.text = it[LocalDataDao.INSTANCE.getProbeAlarm1(mAddresses)].ringName
            probe_2.text = it[LocalDataDao.INSTANCE.getProbeAlarm2(mAddresses)].ringName
            probe_3.text = it[LocalDataDao.INSTANCE.getProbeAlarm3(mAddresses)].ringName
            probe_4.text = it[LocalDataDao.INSTANCE.getProbeAlarm4(mAddresses)].ringName
        })
    }

    private fun setListener() {
        backButton.setOnClickListener {
            println("123-> exit SettingFragment")
            requireActivity().onBackPressed()
        }

        switchNotice.setOnClickListener {
            switchNotice.isSelected = !switchNotice.isSelected
            LocalDataDao.INSTANCE.setDisconnectNotice(switchNotice.isSelected,mAddresses)
        }

        switchAlarm.setOnClickListener {
            switchAlarm.isSelected = !switchAlarm.isSelected
            LocalDataDao.INSTANCE.setAlarm(switchAlarm.isSelected,mAddresses)
        }

        switchAlarmVibration.setOnClickListener {
            switchAlarmVibration.isSelected = !switchAlarmVibration.isSelected
            LocalDataDao.INSTANCE.setAlarmVibration(switchAlarmVibration.isSelected,mAddresses)
        }

        linear9.setOnClickListener {
            parentFragmentManager.beginTransaction()
                .setCustomAnimations(
                    R.anim.translate_into, R.anim.translate_out,
                    R.anim.translate_into_pop, R.anim.translate_out_exit
                )
                .add(
                    R.id.mainContent,
                    FAQSFragment.newInstance()
                )
                .addToBackStack("main")
                .commit()
        }

        linear7.setOnClickListener {
            val dialog = TempPreAlarmBottomDialog()
            dialog.setInitPosition(LocalDataDao.INSTANCE.getTempPreAlarm(mAddresses))
            dialog.showDialog(childFragmentManager)
            dialog.setOnItemSelectListener(object : TempPreAlarmBottomDialog.OnItemSelectListener {
                override fun onSelected(index: Int, indexString: String) {
                    preAlarmType.text = indexString
                    LocalDataDao.INSTANCE.setTempPreAlarm(index,mAddresses)
                }

            })

        }

        linear3.setOnClickListener {
            val dialog = ProbeAlarmBottomDialog()
            dialog.setSelectPosition(LocalDataDao.INSTANCE.getProbeAlarm1(mAddresses))
            dialog.showDialog(childFragmentManager)
            dialog.setOnItemSelectListener(object : ProbeAlarmBottomDialog.OnItemSelectListener {
                override fun onSelected(index: Int, indexString: String) {
                    probe_1.text = indexString
                    LocalDataDao.INSTANCE.setProbeAlarm1(index,mAddresses)
                }

            })
        }

        linear4.setOnClickListener {
            val dialog = ProbeAlarmBottomDialog()
            dialog.setSelectPosition(LocalDataDao.INSTANCE.getProbeAlarm2(mAddresses))
            dialog.showDialog(childFragmentManager)
            dialog.setOnItemSelectListener(object : ProbeAlarmBottomDialog.OnItemSelectListener {
                override fun onSelected(index: Int, indexString: String) {
                    probe_2.text = indexString
                    LocalDataDao.INSTANCE.setProbeAlarm2(index,mAddresses)
                }

            })
        }

        linear5.setOnClickListener {
            val dialog = ProbeAlarmBottomDialog()
            dialog.setSelectPosition(LocalDataDao.INSTANCE.getProbeAlarm3(mAddresses))
            dialog.showDialog(childFragmentManager)

            dialog.setOnItemSelectListener(object : ProbeAlarmBottomDialog.OnItemSelectListener {
                override fun onSelected(index: Int, indexString: String) {
                    probe_3.text = indexString
                    LocalDataDao.INSTANCE.setProbeAlarm3(index,mAddresses)
                }

            })
        }

        linear6.setOnClickListener {
            val dialog = ProbeAlarmBottomDialog()
            dialog.setSelectPosition(LocalDataDao.INSTANCE.getProbeAlarm4(mAddresses))
            dialog.showDialog(childFragmentManager)
            dialog.setOnItemSelectListener(object : ProbeAlarmBottomDialog.OnItemSelectListener {
                override fun onSelected(index: Int, indexString: String) {
                    probe_4.text = indexString
                    LocalDataDao.INSTANCE.setProbeAlarm4(index,mAddresses)
                }

            })
        }

    }
}